import {Project} from "../entity/Project";
import {ProjectFindService} from "../service/project.find.service";
import {Params, ActivatedRoute, Router} from "@angular/router";
import 'rxjs/add/operator/switchMap';
import { Component, OnInit }      from '@angular/core';
import {ProjectPutService} from "../service/project.put.service";
import {Report} from "../entity/Report";
import {ProjectAddService} from "../service/project.add.service";
import {ProjectDeleteService} from "../service/project.delete.service";
import {ProjectModule} from "../project.module";
import {FileUploader} from "ng2-file-upload";
import {CommonIp} from "../../user/entity/Ip";
import {CommonMsg} from "../../shared/entity/CommonMsg";
import {ProjectStep} from "../entity/ProjectStep";
declare var $: any;
@Component({selector:'project-index',
    templateUrl:'./project-index.html',
    styleUrls: ['./project-index-settings.css','./share.min.css'],

  }

)
export  class ProjectIndexComponent extends CommonIp
implements OnInit{
  public project:Project = new Project();
  public imageUrls:string = "";
  private  uploader:FileUploader;
  public repoUrl = 'https://github.com/Epotignano/ng2-social-share';
  public imageUrl = 'https://avatars2.githubusercontent.com/u/10674541?v=3&s=200';
  private URL = window.location.href;
  private TITLE = this.project.title;
  private DESCRIPTION = this.project.description;
  private SUMMARY = this.project.description;
  private SOURCE = window.location.href;
  public loginUserId:string;
  ngOnInit(): void {

    this.loginUserId =  sessionStorage.getItem("id");
    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.projectService.getProjectInfo(params["id"]))
      .subscribe((project: Project) =>
        {
          this.URL =  window.location.href;

         this.SOURCE = window.location.href;
          this.project = project;
          this.TITLE = this.project.title;
          this.DESCRIPTION = "匠心独创网"+this.project.description;
          this.SUMMARY = this.project.description;
        }
      );

  }
  constructor(private projectService: ProjectFindService,
              private projectPutService:ProjectPutService,
              private projectAddService:ProjectAddService,
              private projectDelectService:ProjectDeleteService,
              private activatedRouter: ActivatedRoute,
              private router:Router)

  {
    super();
    this.uploader = new FileUploader({
      url: this.userIp+"/fileUpload",
      method: "POST",
      itemAlias: "uploadedfile",
      removeAfterUpload:true
    });

  }

  private getProjectInfo(id: string) {
    this.projectService.getProjectInfo(id)
      .then(project => this.project = project);
  }
  public showPicList():Array<string>{
    if(this.project.projectData.showImgs!=null&&this.project.projectData.showImgs!=""){
      let picList = this.project.projectData.showImgs.split(",");
      return picList;
    }else{
      return null;
    }
  }
  public toAddStep(id:string){
    sessionStorage.setItem("Isupdate","false");
    sessionStorage.removeItem("projectStep");
    this.router.navigate(['/project/add-step/',id]);
  }

  selectedFileOnChanged() {
    // 这里是文件选择完成后的操作处理

    if(this.project.projectData.showImgs!=null&&this.project.projectData.showImgs!=""){

      let showImages = this.project.projectData.showImgs.split(",");

      if(showImages.length>=3){
        $.jBox.tip("展示图片最多三张");
        return true;
      }
    }

    this.uploader.queue[0].upload(); // 开始上传

    this.uploader.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status == 200) {
        // 上传文件后获取服务器返回的数据
        let tempRes = JSON.parse(response);
        if(this.project.projectData.showImgs!=null&&this.project.projectData.showImgs!=''){
          this.project.projectData.showImgs +=",";
          this.project.projectData.showImgs += tempRes.fileUrl;
        }else{
          this.project.projectData.showImgs = tempRes.fileUrl;
        }
        this.projectPutService.updateShowPics(this.project.id,this.project.projectData.showImgs);
        this.router.navigate(['/project/index/',this.project.id]);

      }else {
        // 上传文件后获取服务器返回的数据错误
      }
    };


  }
  public toProjectSettings(id:string){
    this.router.navigate(['/project/settings/',id]);
  }
  public UpdateProgect(projectStep:ProjectStep){
    let id =  projectStep.id;

    sessionStorage.setItem("Isupdate","true");
    sessionStorage.setItem("projectStep",JSON.stringify(projectStep));
    this.router.navigate(['/project/add-step/',this.project.id]);

  }
  public deletePic(p:string){
    console.log("begin delete:"+p);
    this.projectDelectService.deleteShowPicById(this.project.id,p).then(
      commonMsg=>{
        this.handleResult(commonMsg);
        $.jBox.tip("图片删除成功!");
        // this.router.navigate(['/project/index/',this.project.id]);
      });
  }
  public DeleteProgect(id:string){
    this.projectDelectService.DeleteProgectStep(id).then(
      commonMsg=>{
        $.jBox.tip("步骤删除成功!");
        this.handleResult(commonMsg);

      });
  }
  public handleResult(commonMsg:CommonMsg<any>){

    this.projectService.getProjectInfo(this.project.id)
      .then((project: Project) =>
        {
          this.project = project;
          this.TITLE = this.project.title;
          this.DESCRIPTION = "匠心独创网"+this.project.description;
          this.SUMMARY = this.project.description;
        }
      );

  }
}
